package net.stock.controller;

import org.springframework.web.servlet.View;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.List;

import net.stock.domain.ItemPrice;

/**
 * @author naim
 * @date Dec 29, 2009
 */
public class JsonView implements View {
    private static final String CONTENT_TYPE_JSON = "application/json";

    private String jsonString;

    public JsonView(String jsonString) {
        this.jsonString = jsonString;
    }

    public String getContentType() {
        return CONTENT_TYPE_JSON;

    }

    public void render(Map map, HttpServletRequest request, HttpServletResponse response) throws Exception {
        byte[] buffer = jsonString.getBytes("UTF-8");

        response.setContentType("application/json");
        response.setContentLength(buffer.length);
        response.setCharacterEncoding("UTF-8");

        response.setHeader("Pragma", "no-cache");
        response.addHeader("Cache-Control", "must-revalidate");
        response.addHeader("Cache-Control", "no-cache");
        response.addHeader("Cache-Control", "no-store");
        response.setDateHeader("Expires", 0);

        response.getOutputStream().write(buffer);
        response.flushBuffer();
    }
}
